Next | Prev | Up | Top | Contents | Index
Process Creation
device driverThere are two system calls that create a process. They differ in that one creates a new address space and the other does not.
Normal Process Creation With fork()
address space:duplicated by <Function>fork()<Default Para Font>process:created with <Function>fork()<Default Para Font><Function>fork()<Default Para Font>The conventional method of creating a new process in UNIX is to issue the fork() system call. It creates a "child" process, which is a copy of the "parent" process that issued the call. The address space of the child is a duplicate of the parent's address space, as are most of its attributes, including its machine register contents. Only the return value of fork() differs. The use of fork() is shown in Example 2-1.
Example 2-1 : Schematic of Using fork()<Function>fork()<Default Para Font>:example
int childProcId;
switch(childProcId = fork())
{
case 0:
{ /* this is executed by the child process */ }
break;
case -1:
{ /* parent process, no child process created */ }
break;
default:
{ /* parent process, child process exists */ }
}
copy on write page statueaddress space:copy on writeIRIX does not physically duplicate all the pages of the parent's address space. That would waste a great deal of time. Instead, the page translation table that defines the child's address space initially refers to the physical pages of the parent's address space. However, the table designates these pages as "copy on write."
Whenever the child process writes into a page, it causes a hardware trap. The kernel then makes a duplicate of that one page so that the child has a unique copy into which it can write. Thus only the pages that are written are copied, and then only when the child uses them.
Address Space Replacement With exec()
process:attributes initialized by <Function>exec()address space:replaced by <Function>exec()<Function>exec()<Default Para Font>The exec() system call is the means by which UNIX "loads a program." This call replaces the entire address space with a new one based on a program image loaded from an executable file. The exec() call also initializes many of the process attributes (refer to the exec(2) reference page for details).
The combination of fork() and exec() suits the needs of a command shell. The way a UNIX command shell launches a program is to fork(), creating a new process. In the new process (case 0 in Example 2-1) it calls exec(), replacing the new address space. As a result, in the great majority of fork() calls, the child's address space is completely replaced before more than one or two of its pages have been copied.
However, fork() is not well-suited to building a program designed as a number of small, cooperating processes--the kind of design that your real-time application needs if it is to exploit multiple CPUs.
Lightweight Process Creation With sproc()
address space:shared by lightweight processesprocess:lightweight. <Italics>See<Default Para Font> lightweight process<$nopage>lightweight process:created with <Function>sproc()<Function>sproc()The sproc() system call is unique to IRIX. It creates a new process that shares its parent's address space. The new process has its own machine registers and its own memory region for its stack. Otherwise, both processes execute concurrently using the same program text and data, and sharing many process attributes. A parent process and its children by sproc() constitute a process group.
lightweight process:preferred for real-time usereal-time program:lightweight processes preferredFor several reasons, you should use sproc() if you structure your real-time application as multiple, cooperating processes:
- lightweight process:less work to createThe kernel does less work to create a process with sproc(). For example, it does not have to build a page table to describe a new address space.
- The parent process can initialize disk files, device files, global data structures, memory-mapped I/O, and other objects, and all these are automatically available to the child processes.
- The parent and all child processes have write access to global data, and can use high-performance semaphores and locks to regulate access.
- There is only one address space to lock into memory, no matter how many processes use it.
Next | Prev | Up | Top | Contents | Index